<?php
header('Content-Type: text/plain');
require_once dirname(__FILE__) . '/../../../incls/php/dbutils.class.php';
require_once dirname(__FILE__) . '/../../../incls/php/miscutils.class.php';
require_once dirname(__FILE__) . '/../../../incls/php/config.php';
session_name(SESSION);
session_start();
$myPdo = DbUtils::createPdoInst();
$action = $_POST['action'];
try {
  $myPdo->beginTransaction();
  switch ($action) {
    case 'remove':
      remove($myPdo);
      break;
    default:
      break;
  }
  $myPdo->commit();
} catch (PDOException $e) {
  echo $e->getMessage();
}

function remove($myPdo) {
  if (!isset($_SESSION[ADMIN])) {
    return;
  }
  $i = MiscUtils::getParam('i', '[]');
  $f = MiscUtils::getParam('f', '[]');
  $folder = "../../../exts/plugins/$f";
  if (file_exists($folder)) {
    MiscUtils::rrmdir($folder);
  }
  $cond_vals = new stdClass();
  $cond_vals->c = 'id = :v1';
  $cond_vals->v = array(':v1' => $i);
  DbUtils::deleteByCond($myPdo, 'pz2_plugin', $cond_vals);
  $cond_vals = new stdClass();
  $cond_vals->c = 'plugin_id = :v1';
  $cond_vals->v = array(':v1' => $i);
  DbUtils::deleteByCond($myPdo, 'pz2_plugin_instance', $cond_vals);
  echo json_encode(array());
}
?>